<!-- eslint-disable vue/no-template-shadow -->
<template>
  <div class="dashboard-container">
    <div class="filter-container">
      <el-row :gutter="10" style="width:100%;margin-bottom:10px">
        <el-col :span="3">
          <el-input
            v-model="listQuery.tid"
            placeholder="系统单号"
            class="input-with-select"
            @keypress.enter.native="reloadList"
          />
        </el-col>
        <el-col :span="3">
          <el-input
            v-model="listQuery.order_no"
            placeholder="来源单号"
            class="input-with-select"
            @keypress.enter.native="reloadList"
          />
        </el-col>
        <el-col :span="3">
          <el-input
            v-model="listQuery.trade_code_real"
            placeholder="真实提单号"
            class="input-with-select"
            @keypress.enter.native="reloadList"
          />
        </el-col>
        <el-col :span="3">
          <el-input
            v-model="listQuery.batch_no"
            disabled
            placeholder="批次号"
            class="input-with-select"
            @keypress.enter.native="reloadList"
          />
        </el-col>

        <el-col :span="3">
          <el-input
            v-model="listQuery.username"
            placeholder="用户昵称"
            class="input-with-select"
            @keypress.enter.native="reloadList"
          />
        </el-col>

        <el-col :span="3">
          <el-select
            v-model="listQuery.status"
            style="width:100%"
            placeholder="状态"
            clearable
            @change="reloadList"
          >

            <el-option
              label="待取号"
              :value="102"
            />
            <el-option
              label="已取号"
              :value="103"
            />
            <el-option
              label="已发货"
              :value="104"
            />
            <el-option
              label="已废弃"
              :value="201"
            />
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-input
            v-model="listQuery.waybill_no"
            placeholder="主/从运单号"
            class="input-with-select"
            @keypress.enter.native="reloadList"
          />
        </el-col>
        <el-col :span="3">
          <el-select
            v-model="listQuery.port"
            style="width:100%"
            placeholder="请选择走件口岸"
            clearable
            @change="reloadList"
          >
            <el-option
              v-for="(item,index) in PortList"
              :key="index"
              :label="item.port_name"
              :value="item.code"
            />
          </el-select>
        </el-col>
      </el-row>

      <el-row :gutter="10" style="width:100%;">

        <el-col :span="2">
          <el-select
            v-model="listQuery.channel_id"
            style="width:100%"
            placeholder="请选择渠道"
            clearable
            @change="reloadList"
          >
            <el-option
              v-for="(item,index) in channelList"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-col>
        <el-col :span="2">
          <el-select
            v-model="listQuery.is_error"
            style="width:100%"
            placeholder="异常订单"
            clearable
            @change="reloadList"
          >

            <el-option
              label="是"
              :value="true"
            />
            <el-option
              label="否"
              :value="false"
            />
          </el-select>
        </el-col>
        <el-col :span="2">
          <el-select
            v-model="listQuery.is_collect"
            style="width:100%"
            placeholder="是否揽收"
            clearable
            @change="reloadList"
          >

            <el-option
              label="是"
              :value="true"
            />
            <el-option
              label="否"
              :value="false"
            />
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select
            v-model="listQuery.is_complete"
            style="width:100%"
            placeholder="是否完善"
            clearable
            @change="reloadList"
          >

            <el-option
              label="是"
              :value="true"
            />
            <el-option
              label="否"
              :value="false"
            />
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select
            v-model="listQuery.is_pay"
            style="width:100%"
            placeholder="是否支付"
            clearable
            @change="reloadList"
          >

            <el-option
              label="是"
              :value="true"
            />
            <el-option
              label="否"
              :value="false"
            />
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select
            v-model="listQuery.size"
            style="width:100%"
            placeholder="大小件"
            clearable
            @change="reloadList"
          >
            <el-option
              label="小件"
              :value="1"
            />
            <el-option
              label="大件"
              :value="2"
            />
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select
            v-model="listQuery.is_print"
            style="width:100%"
            placeholder="是否打印"
            clearable
            @change="reloadList"
          >

            <el-option
              label="是"
              :value="1"
            />
            <el-option
              label="否"
              :value="0"
            />
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-select
            v-model="listQuery.is_print_label"
            style="width:100%"
            placeholder="是否标签打印"
            clearable
            @change="reloadList"
          >

            <el-option
              label="是"
              :value="1"
            />
            <el-option
              label="否"
              :value="0"
            />
          </el-select>
        </el-col>

        <el-col :span="3">
          <el-date-picker
            v-model="listQuery.created"
            style="width: 100%;"
            type="datetimerange"
            range-separator="~"
            value-format="yyyy-MM-dd HH:mm:ss"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
          />
        </el-col>

        <el-col :span="20">
          <div style="display:flex;">
            <el-button
              style="margin-top:10px"
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="reloadList"
            >搜索</el-button>
            <el-button
              style="margin-top:10px"
              class="filter-item"
              type="primary"
              @click="getNumBatch()"
            >
              取末端单号
            </el-button>
            <el-button
              style="margin-top:10px"
              class="filter-item"
              type="primary"
              @click="open()"
            >
              D82订单补全
            </el-button>
            <download-excel
              style="margin-left:10px;margin-top:10px"
              class="export-excel-wrapper"
              :fetch="importPackageList"
              :fields="json_fields"
              type="xls"
              worksheet="My Worksheet"
              name="订单导出"
              :before-generate="startDownload"
              :before-finish="finishDownload"
            >
              <el-button type="primary">导出订单</el-button>
            </download-excel>
            <el-button
              size="mini"
              type="primary"
              style="margin-top:10px;margin-left:10px"
              class="filter-item"
              @click="submitPrint()"
            >
              打印面单
            </el-button>
            <el-button
              size="mini"
              type="primary"
              style="margin-top:10px;margin-left:10px"
              class="filter-item"
              @click="submitPrintMark()"
            >
              打印小标签
            </el-button>
            <div style="position:relative;top:10px;left:10px">
              <el-select v-model="printerName" placeholder="请选择打印机" size="mini" style="width: 250px;">
                <el-option v-for="item in printerList" :key="item.name" :label="item.name" :value="item.name" />
              </el-select>
            </div>
            <!-- <el-upload
              ref="upload"
              class="upload-demo"
              accept=".xls,.xlsx"
              action="https://localhost:8080/posts/"
              :on-change="upload"
              :show-file-list="false"
              :auto-upload="false"
            >
              <el-button
                slot="trigger"
                icon="el-icon-upload"
                type="primary"
              >导入地址</el-button>
            </el-upload>
            <el-button
              class="filter-item"
              type="warning"
              icon="el-icon-download"
              style="margin-left:10px;margin-top:10px"
              @click="downLoadModel"
            >下载模板</el-button> -->
          </div>

        </el-col>

      </el-row>
    </div>
    <div style="padding:0 20px">

      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        height="calc(100vh - 280px)"
        style="width: 100%;"
        size="mini"
        border
        tooltip-effect="dark"
        :header-cell-style="{ background: '#eaf1fd', color: '#333' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="40"
        />
        <el-table-column
          label="系统订单号"
          width="200px"
          fixed
        >
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.tid }}</span>
            </div>
          </template>

        </el-table-column>
        <el-table-column
          label="主运单号"
          width="150px"
        >
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.first_waybill_no }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="从运单号"
          width="150px"
        >
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.second_waybill_no }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          width="100px"
        >
          <template slot-scope="{ row }">
            <div>
              <el-tag :type="row.status == 102?'info':row.status == 103?'':row.status == 104?'success':'danger'">{{ row.status == 102?'待取号':row.status == 103?'已取号':row.status == 104?'已发货':'已废弃' }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="订单标识"
          width="160px"
        >
          <template slot-scope="{ row }">
            <div>
              <el-tag style="margin-right:10px" :type="row.label_data.is_error?'success':'info'">异</el-tag>
              <el-tag style="margin-right:10px" :type="row.label_data.is_collect?'success':'info'">揽</el-tag>
              <el-tag style="margin-right:10px" :type="row.label_data.is_complete?'success':'info'">完</el-tag>
              <el-tooltip v-if="row.error" class="item" effect="dark" :content="row.error" placement="top">
                <el-tag style="margin-right:10px;cursor:pointer" :type="row.label_data.is_pay?'success':'info'">支</el-tag>
              </el-tooltip>
              <el-tag v-else style="margin-right:10px;" :type="row.label_data.is_pay?'success':'info'">支</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="用户昵称"
          prop="user.username"
          min-width="180px"
        />
        <el-table-column
          label="客户昵称"
          prop="client.username"
          width="150px"
        />
        <el-table-column
          label="来源单号"
          prop="order_no"
          min-width="150px"
        />
        <el-table-column
          label="提单号"
          prop="trade_code"
          min-width="150px"
        />
        <el-table-column
          label="真实提单号"
          prop="customs_affairs.code"
          min-width="150px"
        />
        <el-table-column
          label="合并提单号"
          prop="customs_affairs.merge_code"
          min-width="150px"
        />
        <el-table-column
          label="收件人"
          prop="receiver.name"
          width="140px"
        />
        <el-table-column
          label="走件渠道"
          prop="channel.name"
          width="240px"
        />
        <el-table-column
          label="货物类型"
          prop="goods_type.name"
          width="140px"
        />
        <el-table-column
          label="价格模版"
          prop="freight_temp.name"
          width="240px"
        />
        <el-table-column
          label="大小件"
          prop="currency"
          width="140px"
        >
          <template slot-scope="{ row }">
            <div>
              <span v-if="row.size == 1">小件</span>
              <span v-if="row.size == 2">大件</span>
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column
          label="订单类型"
          prop="currency"
          width="140px"
        >
          <template slot-scope="{ row }">
            <div>
              <span v-if="row.order_type == 1">仓储</span>
              <span v-if="row.order_type == 2">非仓储</span>
            </div>
          </template>
        </el-table-column> -->
        <!-- <el-table-column
          label="送货方式"
          prop="currency"
          width="140px"
        >
          <template slot-scope="{ row }">
            <div>
              <span v-if="row.delivery_method == 1">上门取件</span>
              <span v-if="row.delivery_method == 2">自送货物</span>
            </div>
          </template>
        </el-table-column> -->
        <el-table-column
          label="快递产品类型"
          prop="currency"
          width="140px"
        >
          <template slot-scope="{ row }">
            <div>
              <span v-if="row.express_type == 1">标快</span>
              <span v-if="row.express_type == 2">特快</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="批次号"
          prop="batch_no"
          width="140px"
        />

        <el-table-column
          label="下单时间"
          prop="created"
          width="140px"
        />
        <el-table-column
          label="主运单打印次数"
          prop="print_first_count"
          width="110px"
        />
        <el-table-column
          label="从运单打印次数"
          prop="print_second_count"
          width="110px"
        />
        <el-table-column
          label="标签打印次数"
          prop="print_label_count"
          width="100px"
        />
        <el-table-column
          label="操作"
          align="right"
          min-width="350"
          class-name="small-padding fixed-width"
          fixed="right"
        >
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type=""
              @click="cheack(row)"
            >
              查看
            </el-button>
            <el-button
              size="mini"
              type="primary"
              @click="edit(row)"
            >
              编辑
            </el-button>
            <el-button
              v-if="!row.second_waybill_no && !row.label_data.is_mother && row.size == 2"
              size="mini"
              type="primary"
              @click="getNum(row)"
            >
              取末端单号
            </el-button>
            <el-button
              v-if="row.status == 104 && !row.label_data.is_pay && !row.label_data.is_mother"
              type="primary"
              @click="payOrder(row)"
            >
              提交支付
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
    <Detial :detial="detial" :status="status" :visible="visible" @update="closeDraw" @reload="reloadList" />
    <el-dialog
      title="取末端订单号"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="handleClose"
    >
      <div style="padding:20px">
        快递公司:
        <el-select
          v-model="company_code"
          style="width:80%"
          placeholder="请选择快递公司"
          clearable
        >
          <el-option
            v-for="(item,index) in CourierList"
            :key="index"
            :label="item.company_name"
            :value="item.company_code"
          />
        </el-select>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitNum">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="批量取末端单号"
      :visible.sync="dialogGetNumBatch"
      width="500px"
      :before-close="handleClose"
    >
      <div style="padding:20px">
        <el-progress :percentage="((getNumBatchSucNum + getNumBatchErrNum) / selectList.length * 100).toFixed()" />

        <div style="font-size: 16px; margin-top: 15px;">
          共 {{ selectList.length }} 单，成功 {{ getNumBatchSucNum }} 单，失败 {{ getNumBatchErrNum }} 单
        </div>
        <div v-if="getNumBatchErrList.length > 0" style="font-size: 16px; margin-top: 15px;">
          <el-alert
            v-for="(item,index) in getNumBatchErrList"
            :key="index"
            :title="item.msg + ' X ' +item.num"
            :closable="false"
            style="margin-top: 10px;"
            type="error"
            show-icon
          />
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogGetNumBatch = false;dialogVisible = false;getList()">确 定</el-button>
      </span>
    </el-dialog>
    <Upload :visible="editAllDraw" @update="closeDraw" @reload="reloadList" />
    <el-dialog
      title="重复打印检测"
      :visible.sync="cheackRes"
      width="500px"
      :before-close="handleClose"
    >
      <div style="padding:20px">
        <div style="font-size: 16px; margin-top: 15px;">
          以下主运单号重复打印，请确认继续打印
        </div>
        <div v-if="errorData.length > 0" style="font-size: 16px; margin-top: 15px;">
          <el-alert
            v-for="(item,index) in errorData"
            :key="index"
            :title="item"
            :closable="false"
            style="margin-top: 10px;"
            type="error"
            show-icon
          />
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cheackRes = false;">取 消</el-button>
        <el-button type="primary" @click="goOnPrint()">继续打印</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listAllOrder, pushPay, getSecondWaybillNo, printTimes, orderLabel, checkExpress } from '@/api/order/index'
import { listPort } from '@/api/base/port'
import { listCourier } from '@/api/base/express'
import { listChannel } from '@/api/vocational/irrigation'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import Detial from './components/orderDetial.vue'
import _ from 'lodash'
import Upload from '@/components/Upload'
import { doConnect, doPrintHandover, doPrint } from '@/utils/print'
import { expressLabel } from '@/api/order/index'
import { goodsList } from '@/api/base/goods'

export default {
  name: 'Dashboard',
  components: {
    Pagination,
    Detial,
    Upload
  },
  data() {
    return {
      errorData: [],
      cheackRes: false,
      editAllDraw: false,
      loadingExcel: false,
      dialogVisible: false,
      dialogGetNumBatch: false, // 批量取末端单号弹窗
      data_arr: [],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        tid: '',
        order_no: '',
        trade_code: '',
        batch_no: '',
        username: '',
        status: '',
        waybill_no: '',
        created: [],
        port: '',
        is_complete: '',
        is_error: '',
        is_collect: '',
        is_pay: '',
        size: '',
        trade_code_real: ''
      },
      visible: false,
      status: 1,
      id: '',
      detial: {},
      PortList: [],
      row: {},
      CourierList: [],
      company_code: '',
      selectList: [],
      getNumBatchSucNum: 0, // 批量取末端单号-成功的数量
      getNumBatchErrNum: 0, // 批量取末端单号-失败的数量
      getNumBatchErrList: [], // 批量取末端单号-失败原因
      submitGetNumType: 1, // 默认为单个取末端单号， 2 为批量
      goodsCateList: [],

      json_fields: {
        系统订单号: {
          field: 'tid',
          callback: value => {
            return '&nbsp;' + value
          }
        },
        集包号: {
          field: 'package_number',
          callback: value => {
            return '&nbsp;' + value
          }
        },
        主运单号: 'first_waybill_no',
        从运单号: 'second_waybill_no',
        状态: {
          field: 'status',
          // 自定义回调函数
          callback: value => {
            if (value === 102) return '待取号'
            if (value === 103) return '已取号'
            if (value === 104) return '已发货'
            if (value === 201) return '已废弃'
          }
        },
        用户昵称: 'user.username',
        客户昵称: 'client.username',
        客户唛头: 'receiver.user_mark',
        来源单号: 'order_no',
        提单号: 'trade_code',
        真实提单号: 'customs_affairs.code',
        合并提单号: 'customs_affairs.merge_code',
        收件人: 'receiver.name',
        电话: 'mobile',
        地址: 'address',
        邮编: 'receiver.postcode',
        走件渠道: 'channel.name',
        货物类型: 'goods_type.name',
        大小件: {
          field: 'size',
          // 自定义回调函数
          callback: value => {
            if (value === 0) return '未选择'
            if (value === 1) return '小件'
            if (value === 2) return '大件'
          }
        },
        // 订单类型: {
        //   field: 'order_type',
        //   callback: value => {
        //     if (value === 0) return '未选择'
        //     if (value === 1) return '仓储'
        //     if (value === 2) return '非仓储'
        //   }
        // },
        // 送货方式: {
        //   field: 'delivery_method',
        //   callback: value => {
        //     if (value === 0) return '未选择'
        //     if (value === 1) return '上门取件'
        //     if (value === 2) return '自送货物'
        //   }
        // },
        快递产品类型: {
          field: 'express_type',
          // 自定义回调函数
          callback: value => {
            if (value === 0) return '未选择'
            if (value === 1) return '标快'
            if (value === 2) return '特快'
          }
        },
        批次号: 'batch_no',
        '揽件重量(kg)': 'weight_pickup',
        '发运重量(kg)': 'weight_send',
        '结算重量(kg)': 'weight',
        账单金额: 'pay_cost',
        下单时间: 'created',
        主运单打印次数: 'print_first_count',
        从运单打印次数: 'print_second_count',
        标签打印次数: 'print_label_count'
      },
      json_data: [],
      printerName: '',
      printerList: [],
      printLoading: '',
      back: '',
      channelList: [],
      printType: ''

    }
  },
  computed: {
  },
  watch: {
    back(val) {
      // 打印回调
      printTimes({ document_id: val.printStatus[0].documentID, type: this.printType }).then(res => {

      })
      this.printLoading.close()
    }
  },
  mounted() {
    var that = this
    listChannel({
      status: 1,
      limit: 999999
    }).then(res => {
      this.channelList = res.data.data
    })
    goodsList({
      cate: 0
    }).then(res => {
      this.goodsCateList = res.data.list.map((i) => {
        return i.name
      })
      this.goodsCateList.forEach(item => {
        this.json_fields[item] = item
      })
    })
    const socket = doConnect()
    socket.onmessage = function(event) {
      console.log(JSON.parse(event.data))
      const res = JSON.parse(event.data)
      if (res.cmd === 'getPrinters') {
        that.printerName = res.defaultPrinter // 默认打印机
        that.printerList = res.printers
      }
      if (res.cmd === 'print') {
        that.printLoading = that.$loading({
          lock: true,
          text: '打印中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      }
      if (res.cmd === 'notifyPrintResult' && res.taskStatus === 'printed') {
        that.back = res
      }
    }
  },
  created() {
    this.listQuery.batch_no = this.$route.query.batch_no
    this.getList()
    this.getPortList()
    this.getlistCouriers()
  },
  methods: {
    print(waybill_no) {
      return new Promise((resolve, reject) => {
        expressLabel({
          waybill_no: waybill_no
        }).then(res => {
          resolve(res.data)
        })
      })
    },
    printMark(id) {
      return new Promise((resolve, reject) => {
        orderLabel({
          id: [id]
        }).then(res => {
          resolve(res.data)
        })
      })
    },
    checkExpressOrders() {
      const waybill_nos = this.selectList.map(item => {
        return item.first_waybill_no
      })
      return new Promise((resolve, reject) => {
        checkExpress({
          waybill_nos: waybill_nos
        }).then(res => {
          resolve(res.data.errorData)
        })
      })
    },
    // 打印小便签
    async submitPrintMark() {
      if (!this.selectList.length) return this.$message.error('请勾选要打印的数据')
      for (let index = 0; index < this.selectList.length; index++) {
        const element = this.selectList[index]
        const data = await this.printMark(element.id)
        this.printType = 'label'
        doPrint(data.list, data.templateURL, this.printerName)
      }
    },
    // 开始打印
    async submitPrint() {
      if (!this.selectList.length) return this.$message.error('请勾选要打印的数据')
      this.errorData = await this.checkExpressOrders()
      if (this.errorData.length) {
        this.cheackRes = true
      } else {
        for (let index = 0; index < this.selectList.length; index++) {
          const element = this.selectList[index]
          const data = await this.print(element.first_waybill_no)
          const arr = []
          arr.push(data)
          this.printType = ''
          doPrintHandover(arr, this.printerName)
        }
      }
    },

    async goOnPrint() {
      this.cheackRes = false
      for (let index = 0; index < this.selectList.length; index++) {
        const element = this.selectList[index]
        const data = await this.print(element.first_waybill_no)
        const arr = []
        arr.push(data)
        this.printType = ''
        doPrintHandover(arr, this.printerName)
      }
    },
    handleSelectionChange(val) {
      this.selectList = val
    },
    getlistCouriers() {
      listCourier({
        limit: 1000,
        status: 1
      }).then(res => {
        this.CourierList = res.data.data
      })
    },
    getNum(row) {
      this.dialogVisible = true
      this.company_code = ''
      this.row = row
      this.submitGetNumType = 1 // 单次操作取末端单号
    },
    getNumBatch() {
      if (!this.selectList.length) {
        this.$message({
          type: 'error',
          message: '请选择要取号的订单'
        })
        return
      }
      // eslint-disable-next-line no-unused-vars
      let size = true
      this.selectList.forEach(item => {
        if (item.size === 1) {
          // eslint-disable-next-line no-const-assign
          size = false
        }
      })
      if (!size) {
        this.$message({
          type: 'error',
          message: '订单中不能包含小件订单'
        })
        return
      }
      this.dialogVisible = true
      this.company_code = ''
      this.submitGetNumType = 2 // 批量操作
      this.getNumBatchSucNum = 0
      this.getNumBatchErrNum = 0
      this.getNumBatchErrList = []
    },
    handleClose() {
      this.dialogVisible = false
    },
    submitNum() {
      if (this.submitGetNumType === 2) {
        // 批量
        this.dialogGetNumBatch = true
        this.selectList.forEach(value => {
          getSecondWaybillNo({
            tag_batch_option: true,
            tid: value.tid,
            company_code: this.company_code
          }).then(res => {
            if (res.sub_code === 'success') {
              // 成功
              this.getNumBatchSucNum++
            } else {
              this.getNumBatchErrNum++
              const index = this.existErrorMsg(res.msg)
              if (index === false) {
                this.getNumBatchErrList.push({ msg: res.msg, num: 1 })
              } else {
                this.getNumBatchErrList[index].num++
              }
            }
          })
        })
      } else {
        getSecondWaybillNo({
          tid: this.row.tid,
          company_code: this.company_code
        }).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.dialogVisible = false
          this.getList()
        })
      }
    },
    // 口岸列表
    getPortList() {
      listPort({
        limit: 1000,
        status: 1
      }).then(res => {
        this.PortList = res.data.data
      })
    },
    existErrorMsg(msg) {
      if (this.getNumBatchErrList.length <= 0) {
        return false
      }
      let flag = false
      this.getNumBatchErrList.forEach((item, index) => {
        if (item.msg === msg) {
          flag = index
        }
      })
      return flag
    },
    closeDraw() {
      this.visible = false
      this.editAllDraw = false
    },
    addNews() {
      this.status = 1
      this.visible = true
    },
    edit(row) {
      this.detial = { ...row }
      this.status = 2
      this.visible = true
    },
    cheack(row) {
      this.detial = { ...row }
      this.status = 1
      this.visible = true
    },
    payOrder(row) {
      var that = this
      this.$confirm('支付此订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        pushPay({
          tids: [row.tid]
        }).then(res => {
          that.$message({
            type: 'success',
            message: res.msg
          })
          that.getList()
        })
      }).catch(() => {

      })
    },
    reloadList() {
      this.listQuery.page = 1
      this.visible = false
      this.editAllDraw = false
      this.getList()
    },
    getList() {
      this.listLoading = true
      listAllOrder(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.count
        this.listLoading = false
      })
    },
    // 导出
    getPageSize() {
      return new Promise((resolve, reject) => {
        listAllOrder(this.listQuery).then(response => {
          resolve(response.data.count)
        })
      })
    },
    getExcelList(page) {
      const param = _.cloneDeep(this.listQuery)
      param.page = page
      return new Promise((resolve, reject) => {
        listAllOrder(param).then(response => {
          response.data.list.forEach(item => {
            item.mobile = '+' + _.get(item.receiver, 'area_code', '') + ' ' + _.get(item.receiver, 'mobile', '')
            item.address = _.get(item.receiver, 'nation', '') + _.get(item.receiver, 'province', '') + _.get(item.receiver, 'city', '') + _.get(item.receiver, 'district', '') + _.get(item.receiver, 'detailed', '')
            if (item.package_no) {
              item.package_number = item.package_no
            } else {
              item.package_number = item.first_waybill_no
            }
            this.goodsCateList.forEach(i => {
              item[i] = ''
              item.item.forEach((x) => {
                if (x.cate2 && x.cate2.name === i) {
                  item[i] = x.num
                }
              })
            })
          })
          resolve(response.data.list)
        })
      })
    },
    async importPackageList() {
      const total = await this.getPageSize()
      const page = Math.ceil(total / this.listQuery.limit)
      var data = []
      for (let i = 1; i <= page; ++i) {
        data = data.concat(await this.getExcelList(i))
      }
      return data
    },
    startDownload() {
      this.loadingExcel = this.$loading({
        lock: true,
        text: '导出表格中，请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
    },
    finishDownload() {
      this.loadingExcel.close()
    },
    open() {
      this.editAllDraw = true
    }
  }
}
</script>
<style scoped>
.filter-container{
  padding:20px 20px 10px;
}
.upload-demo{
  margin: 20px 0 10px;
}
</style>
